package leetcode;

public class l0036_有效的数独_解法2_数组 {
    public boolean isValidSudoku(char[][] board) {
        boolean[][] rows = new boolean[9][9];
        boolean[][] cols = new boolean[9][9];
        boolean[][] boxes = new boolean[9][9];

        for (int row = 0; row < 9; row++) {
            for (int col = 0; col < 9; col++) {
                char num = board[row][col];
                if (num == '.') {
                    continue;
                }
                num = (char) (num - '1');
                if (rows[row][num]) {
                    return false;
                }
                if (cols[col][num]) {
                    return false;
                }
                int boxIndex = (row / 3) * 3 + col / 3;
                if (boxes[boxIndex][num]) {
                    return false;
                }
                rows[row][num] = true;
                cols[col][num] = true;
                boxes[boxIndex][num] = true;
            }
        }
        return true;
    }
}
